package org.domeos.framework.api.biz.resource.impl;

import org.domeos.framework.api.biz.base.impl.BaseBizImpl;
import org.domeos.framework.api.biz.resource.ResourceBiz;
import org.domeos.framework.api.mapper.resource.ResourcesMapper;
import org.domeos.framework.api.model.auth.related.Role;
import org.domeos.framework.api.model.resource.Resource;
import org.domeos.framework.api.model.resource.related.ResourceOwnerType;
import org.domeos.framework.api.model.resource.related.ResourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by feiliu206363 on 2016/4/5.
 */
@Service("resourceBiz")
public class ResourceBizImpl extends BaseBizImpl implements ResourceBiz {
    @Autowired
    ResourcesMapper resourcesMapper;

    @Override
    public void save(int resourceId, ResourceType resourceType, int ownerId, ResourceOwnerType resourceOwnerType,
                     Role role) {
        Resource resource = new Resource();
        resource.setResourceId(resourceId);
        resource.setResourceType(resourceType);
        resource.setOwnerId(ownerId);
        resource.setOwnerType(resourceOwnerType);
        resource.setRole(role);
        resource.setUpdateTime(System.currentTimeMillis());

        resourcesMapper.save(resource);
    }

    @Override
    public void save(Resource resource) {
        resourcesMapper.save(resource);
    }

    @Override
    public Resource getByUserAndResourceId(int userId, int resourceId, ResourceType resourceType) {
        return resourcesMapper.getByUserAndResourceId(userId, ResourceOwnerType.USER, resourceId, resourceType);
    }

    @Override
    public Resource getGroupResourceByResourceId(int resourceId, ResourceType resourceType) {
        return resourcesMapper.getGroupResourceByResourceId(ResourceOwnerType.GROUP, resourceId, resourceType);
    }

    @Override
    public List<Resource> listByType(ResourceType resourceType) {
        return resourcesMapper.listByType(resourceType);
    }

    @Override
    public List<Resource> listByUserId(int userId, ResourceType resourceType) {
        return resourcesMapper.listByOwnerId(userId, ResourceOwnerType.USER, resourceType);
    }

    @Override
    public List<Resource> getGroupListByUserId(int userId, ResourceType resourceType) {
        return resourcesMapper.listGroupResourceListByUserId(userId, ResourceOwnerType.GROUP, resourceType);
    }

    @Override
    public List<Resource> getByResourceIdAndType(int resourceId, ResourceType type) {
        return resourcesMapper.listByResourceIdAndType(resourceId, type);
    }

    @Override
    public List<Resource> listByOwnerId(int groupId, ResourceOwnerType type, ResourceType project) {
        return resourcesMapper.listByOwnerId(groupId, type, project);
    }

    @Override
    public void deleteByIdAndType(int resourceId, ResourceType type) {
        resourcesMapper.deleteByIdAndType(type, resourceId);
    }

    @Override
    public List<Resource> getGroupByUserId(int id, String name, ResourceType resourceType) {
        return resourcesMapper.listGroupResourceByUserId(id, name, resourceType);
    }

    @Override
    public void delete(ResourceType resourceType, int resourceId, ResourceOwnerType ownerType, int ownerId) {
        resourcesMapper.delete(resourceType, resourceId, ownerType, ownerId);
    }

    @Override
    public Resource get(int userId, ResourceOwnerType user, int resourceId, ResourceType resourceType) {
        return resourcesMapper.get(userId, user, resourceId, resourceType);
    }

    @Override
    public Resource getGroup(String name, int resourceId, ResourceType resourceType) {
        return resourcesMapper.getGroupResource(name, resourceId, resourceType);
    }

    @Override
    public void modify(Resource resource) {
        resourcesMapper.modify(resource);
    }
}
